/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.camelspit.preludetoarmageddon.data;
import com.camelspit.preludetoarmageddon.entity.*;
import com.camelspit.preludetoarmageddon.util.*;
import java.io.*;
import java.util.*;
import java.net.*;
/**
 *
 * @author DMK
 */
public class PlayerDatabase extends EntityVectorDatabase<Player>{

    private static final String playersTxtLoc =
            "/com/camelspit/preludetoarmageddon/data/players/";
    public PlayerDatabase() {
    }

    public boolean load() throws FileNotFoundException {
            Scanner input = new Scanner(new File(UtilLib.getFileLocURI(this, 
                    playersTxtLoc + "players.txt")));
            while(input.hasNext()) {
                loadPlayer(input.next().trim());
            }
        input.close();
        return true;
    }

    public boolean save() throws URISyntaxException, IOException {
        File playerDB = new File(UtilLib.getFileLocURI(this,
                playersTxtLoc + "players.txt"));
        Formatter output = new Formatter(playerDB);
        for(int i = 0; i < entities.size(); i++) {
            output.format("%s%s%n", entities.get(i).name(), ".plr");
            entities.get(i).save();
        }
        output.close();
        return true;
    }

    public boolean addPlayer(Player player) throws URISyntaxException, IOException {
        Iterator playerItr = entities.iterator();
        while(playerItr.hasNext()) {
            if(((Player)playerItr.next()).name().equalsIgnoreCase(player.name()))
                return false;
        }
        entities.add(player);
        savePlayer(player);
        FileWriter playerWriter = new FileWriter(new File(
                UtilLib.getFileLocURI(this, playersTxtLoc + "players.txt")));
        PrintWriter writer = new PrintWriter(playerWriter);
        for(int i = 0; i < entities.size(); i++) {
            writer.println(entities.get(i).name() + ".plr");

        }
        writer.close();
        return true;
    }

    public synchronized void loadPlayer(String name) throws FileNotFoundException {
        int id;
        Scanner playerInput = new Scanner(new File(name));
        entities.add(new Player());
        id = entities.size() - 1;
        entities.get(id).setPlayerName(playerInput.next());
        entities.get(id).setPassword(playerInput.next());
        entities.get(id).setRank(playerInput.nextInt());
        entities.get(id).setStatPoints(playerInput.nextInt());
        entities.get(id).setExperience(playerInput.nextInt());
        entities.get(id).setLevel(playerInput.nextInt());
        entities.get(id).setRoom(playerInput.nextInt());
        entities.get(id).money().setMoney(playerInput.nextInt());
        entities.get(id).setHitPoints(playerInput.nextInt());
        entities.get(id).setNextAttackTime(playerInput.nextInt());
        for(Attributes attr: Attributes.values()) {
            entities.get(id).setBaseAttr(attr, playerInput.nextInt());
        }
        //close file
        playerInput.close();
    }

    /**
     * Saves a single player.
     *
     * @param playerID
     */
    public void savePlayer(String playerName) throws URISyntaxException, IOException {
        for(int i = 0; i < entities.size(); i++) {
            if(entities.get(i).name().equalsIgnoreCase(playerName))
                entities.get(i).save();
        }
    }
    public void savePlayer(Player player) {
        try {
            for(int i = 0; i < entities.size(); i++) {
                if(entities.get(i).name().equalsIgnoreCase(player.name()))
                    entities.get(i).save();
            }
        } catch (URISyntaxException URIe) {
            return; //add log message
        } catch(IOException IOe) {
            return; //add log message
        }
    }

    public void logout(Player player) {
        //add log message
        player.setLoggedIn(false);
        player.setActive(false);
        savePlayer(player);
    }

}
/**    static int lastID() {

    }

    //static iterator findactive()

    //static iterator findloggedin()
**/
